package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.Constant;
import com.ruoyi.tb.lmt.domain.bo.ClientLogBo;
import com.ruoyi.tb.lmt.domain.bo.KeepAliveBo;
import com.ruoyi.tb.lmt.service.IClientLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.ClientRegistryBo;
import com.ruoyi.tb.lmt.domain.vo.ClientRegistryVo;
import com.ruoyi.tb.lmt.domain.ClientRegistry;
import com.ruoyi.tb.lmt.mapper.ClientRegistryMapper;
import com.ruoyi.tb.lmt.service.IClientRegistryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 业务工具Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class ClientRegistryServiceImpl implements IClientRegistryService {

    private final ClientRegistryMapper baseMapper;

    /**
     * 查询业务工具
     */
    @Override
    public ClientRegistryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询业务工具列表
     */
    @Override
    public TableDataInfo<ClientRegistryVo> queryPageList(ClientRegistryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClientRegistry> lqw = buildQueryWrapper(bo);
        Page<ClientRegistryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务工具列表
     */
    @Override
    public List<ClientRegistryVo> queryList(ClientRegistryBo bo) {
        LambdaQueryWrapper<ClientRegistry> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClientRegistry> buildQueryWrapper(ClientRegistryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClientRegistry> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, ClientRegistry::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getClientCode()), ClientRegistry::getClientCode, bo.getClientCode());
        lqw.like(StringUtils.isNotBlank(bo.getClientName()), ClientRegistry::getClientName, bo.getClientName());
        lqw.eq(StringUtils.isNotBlank(bo.getClientAddress()), ClientRegistry::getClientAddress, bo.getClientAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ClientRegistry::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLimits()), ClientRegistry::getLimits, bo.getLimits());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), ClientRegistry::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), ClientRegistry::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, ClientRegistry::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), ClientRegistry::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, ClientRegistry::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增业务工具
     */
    @Override
    public Boolean insertByBo(ClientRegistryBo bo) {
        ClientRegistry add = BeanUtil.toBean(bo, ClientRegistry.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改业务工具
     */
    @Override
    public Boolean updateByBo(ClientRegistryBo bo) {
        ClientRegistry update = BeanUtil.toBean(bo, ClientRegistry.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ClientRegistry entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业务工具
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public Boolean keepAlive(KeepAliveBo keepAliveBo) {
        ClientRegistryBo bo = new ClientRegistryBo();
        bo.setClientCode(keepAliveBo.getClientCode());
        LambdaQueryWrapper<ClientRegistry> lqw = buildQueryWrapper(bo);
        ClientRegistryVo clientRegistryVo = baseMapper.selectVoOne(lqw);

        if(clientRegistryVo == null){
            ClientRegistryBo insertObj = new ClientRegistryBo();
            insertObj.setClientCode(keepAliveBo.getClientCode());
            insertObj.setClientName(keepAliveBo.getClientName());
            insertObj.setClientAddress(keepAliveBo.getClientAddress());
            insertObj.setLimits(keepAliveBo.getLimits());
            insertObj.setStatus(Constant.ON);
            insertObj.setCreateBy(Constant.ADMIN);
            insertObj.setUpdateBy(Constant.ADMIN);
            insertByBo(insertObj);
            return true;
        }

        String status = clientRegistryVo.getStatus();
        if(status.equals(Constant.ON)){
            return false;
        }
        clientRegistryVo.setStatus(Constant.ON);
        clientRegistryVo.setClientCode(keepAliveBo.getClientCode());
        clientRegistryVo.setClientName(keepAliveBo.getClientName());
        clientRegistryVo.setClientAddress(keepAliveBo.getClientAddress());
        clientRegistryVo.setLimits(keepAliveBo.getLimits());
        ClientRegistryBo bean = BeanUtil.toBean(clientRegistryVo, ClientRegistryBo.class);
        updateByBo(bean);
        return true;
    }
}
